package com.dal;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.sojoys.artifact.factory.bytebuff.ArrayByteBuff;
import com.sojoys.artifact.factory.bytebuff.NettyByteBuff;
import com.sojoys.artifact.manager.ByteBuffManager;
import com.sojoys.artifact.manager.ContextManager;
import com.sojoys.artifact.manager.PropManager;

import org.quartz.TriggerUtils;
import org.quartz.impl.triggers.CronTriggerImpl;

import com.alibaba.fastjson.JSON;
import com.dal.db.DbSave;
import com.dal.db.bean.Account;
import com.dal.db.cache.AccountCache;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xiaoleilu.hutool.lang.Validator;

public class Test {
	
    static ThreadLocal<Integer> intHolder = new ThreadLocal<Integer>();  

    static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();  
	public static void main(String[] args)throws Exception {
//		BufferTest();
		PropManager.use("app.properties");
		// xml配置文件
		ContextManager.me().init("app-init.xml");
		
		List<Account> accounts = AccountCache.me().loadAll();
		for (Account account : accounts) {
			account.setPassword("aaa123");
			account.update();
		}
		
		DbSave.saveAll();
		
		
		Thread.sleep(10000);
	}

	private static void BufferTest(){
		ArrayByteBuff buff = (ArrayByteBuff) ByteBuffManager.me().allocate();
		Map<Integer,List<Integer>> map = Maps.newHashMap();
		List<Integer> list = Lists.newArrayList();
		list.add(1);
		list.add(1);
		list.add(1);
		map.put(2,list);
		buff.writeObject(map);
		Map<Integer,List<Integer>> newMap = (Map<Integer, List<Integer>>) buff.readObject();
		System.out.println(newMap);
		System.out.println(newMap.get(2));
	}
	
	private static void steamTest(){
		List<SteamTest> list= Lists.newArrayList();
		list.add(new SteamTest(22));
		list.add(new SteamTest(11));
		list.add(new SteamTest(44));
		list.add(new SteamTest(33));
		
		List<Integer> lists = list.stream().sorted(Comparator.comparing(st->Integer.valueOf(st.getAbc()))).map(st->st.getAbc()).limit(3).collect(Collectors.toList());
		System.out.println(lists);
	}
	
	private static void threadLocalTest()throws Exception{
		Test.intHolder.set(0);
		cachedThreadPool.execute(new ThreadLocalTest() );
		
		Thread t = new Thread(new ThreadLocalTest());
		t.start();
		Thread.sleep(40);
		System.out.println(Test.intHolder.get());
	}
	
	
	private static void mapTest(){
		Map map = Maps.newHashMap();
		int count = (int) map.computeIfAbsent(1, k -> 20);
		System.out.println(count);
	}

	private static void cronTest() throws ParseException {
		CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
		cronTriggerImpl.setCronExpression("0 0 15 5 * ?");// 这里写要准备猜测的cron表达式
		Calendar calendar = Calendar.getInstance();
		Date now = calendar.getTime();
		calendar.add(Calendar.YEAR, -2);// 把统计的区间段设置为从现在到2年后的今天（主要是为了方法通用考虑，如那些1个月跑一次的任务，如果时间段设置的较短就不足20条)
		List<Date> dates = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, calendar.getTime(), now);// 这个是重点，一行代码搞定~~
		System.out.println(dates.size());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (int i = 0; i < dates.size(); i++) {
			// if (i > 19) {// 这个是提示的日期个数
			// break;
			// }
			System.out.println(dateFormat.format(dates.get(i)));
		}
	}

	public static String add(String s, String ss) {
		return s + "," + ss;
	}

	private static void patternTest() {
		Pattern ACCOUNT_OR_PASSWORD = Pattern.compile("^[a-zA-Z0-9]{5,11}$");

		boolean b = Validator.isMactchRegex(ACCOUNT_OR_PASSWORD, "124fd3");
		System.out.println(b);
	}

	public static boolean isNumeric(String str) {
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public static void jsonTest() {
		List<Integer> list = Lists.newArrayList();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(5);
		list.add(7);
		Map map = Maps.newHashMap();
		Map subMap = Maps.newHashMap();
		subMap.put(1, 1);
		subMap.put(2, 2);
		map.put("map", subMap);
		map.put("list", list);
		String jsonStr = JSON.toJSONString(map);
		System.out.println(jsonStr);
		// JSON.parse
//		List<Integer> list1 = JSON.parseArray(jsonStr, Integer.class);
		
//		System.out.println(list1);
	}
}

class ThreadLocalTest implements Runnable{

	@Override
	public void run() {
		Test.intHolder = new ThreadLocal<>();
		System.out.println("get ="+Test.intHolder.get());
		Test.intHolder.set(1);
		System.out.println(Test.intHolder.get());
	}
	
}

class SteamTest{
	int abc;
	

	public SteamTest(int abc) {
		super();
		this.abc = abc;
	}

	public int getAbc() {
		return abc;
	}

	public void setAbc(int abc) {
		this.abc = abc;
	}
	
}

class OptionalTest {

	static Map<Integer, Map<Integer, Integer>> intMap;

	static {
		intMap = Maps.newHashMap();
		for (int i = 0; i < 10; i++) {
			Map<Integer, Integer> subMap = intMap.computeIfAbsent(i, k -> new HashMap<>());
			for(int j = 0;j<10;j++){
				subMap.put(j, j);	
			}
		}
	}

	Map<Integer, Integer> getMap(int num) {
		return intMap.get(num);
	}
}